<!DOCTYPE html>
<html>

<head>
    <!-- 即使写了base,调用frame window方法时vscode webview还是认为跨域-->
    <base href="http://127.0.0.1:${servicerPort}" />
    <meta charset="UTF-8">
    <meta http-equiv="Content-Security-Policy" content="script-src * 'unsafe-inline'">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        html,
        body {
            height: 100%;
            width: 100%;
            padding: 0px;
            background-color: white;
            border: none;
        }
    </style>

    <script defer>
        const { info } = console;

        function finallyFire(delayMillion, callBack) {
            let args, timer;

            function _call() {
                callBack.apply(null, args);
            }

            return {
                onFire() {
                    if (timer) {
                        clearTimeout(timer);
                        timer = null;
                    }
                    args = [...arguments];
                    timer = setTimeout(_call, delayMillion);
                }
            }
        }

        /**
         * 切换要编辑的控件时,引起webview的颤抖(layout变化造成的),这里加一个延迟相应来减少抖动
         */
        document.onreadystatechange = function () {
            if (document.readyState == "complete") {
                const frame_ide = document.getElementById('frame_ide');
                function setSize() {
                    frame_ide.width = window.innerWidth;
                    frame_ide.height = window.innerHeight;
                }

                setSize();

                const lazyResize = finallyFire(100, () => {
                    setSize();
                });

                window.onresize = function () {
                    lazyResize.onFire();
                }
            }
        };

        function getIDEWindow(){
            return window.frames['frame_ide'];
        }

        window.onfocus = function(){
            const ide = getIDEWindow();
            if(ide){
                ide.postMessage('active', 'http://127.0.0.1:${servicerPort}');
            }
        }

    </script>

</head>

<body>
    <iframe frameborder="0" id="frame_ide" name="frame_ide"
        src='/webview/dist/v2.html?viewPath=views/mxDev/webIDE.entrance&remoteService=/service&mxDev=${params}&scheme=v2'>
    </iframe>
</body>

</html>